﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using SaaS.Model.Dto.Auths;
//using XCore.Logger;
//using SaaS.Model.Entity.Auths;
//using SaaS.Core;
//using System.Data;
//using Web.System.Utils;
//using SaaS.Model.Enums;
//using XCore.Dependency;

//namespace Web.System.Services.Auths
//{
//    public class OrganizationUnitApplicationService : SystemApplicationServiceBase, ITransientDependency
//    {
//        public int CreateOrEdit(OrganizationUnitInput input)
//        {
//            if (input.Id.HasValue)
//            {
//                if (input.ParentId.HasValue)
//                    this.CheckParentOrganizationUnit(input.ParentId.Value);
//                var name = SDC.OrganizationUnits.Where(c => c.Id == input.Id.Value).Select(c => c.Name).FirstOrDefault();
//                if (string.IsNullOrWhiteSpace(name)) throw new MessageException(CoreSetting.Error_ID);
//                if (name != input.Name)
//                    this.CheckOrganizationUnitName(input.Name);
//                Repository.Update<OrganizationUnit, OrganizationUnitInput>(input);
//                return input.Id.Value;
//            }
//            else
//            {
//                if (input.ParentId.HasValue)
//                    this.CheckParentOrganizationUnit(input.ParentId.Value);
//                this.CheckOrganizationUnitName(input.Name);
//                var entity = ObjectMapper.Map<OrganizationUnit>(input);
//                return Repository.InsertAndGetId(entity);
//            }
//        }
//        /// <summary>
//        /// 检查机构
//        /// </summary>
//        /// <param name="exp"></param>
//        /// <returns></returns>
//        private bool CheckOrganizationUnit(Func<OrganizationUnit, bool> exp)
//        {
//            return SDC.OrganizationUnits.Any(c => exp(c));
//        }
//        /// <summary>
//        /// 检查机构名
//        /// </summary>
//        /// <param name="name"></param>
//        /// <param name="tenantId"></param>
//        private void CheckOrganizationUnitName(string name)
//        {
//            if (this.CheckOrganizationUnit(c => c.Name == name))
//                throw new MessageException($"机构名：{name} 已存在");
//        }
//        /// <summary>
//        /// 检查上级机构
//        /// </summary>
//        /// <param name="parentId"></param>
//        /// <param name="tenantId"></param>
//        private void CheckParentOrganizationUnit(int parentId)
//        {
//            if (!this.CheckOrganizationUnit(c => c.Id == parentId))
//                throw new MessageException("上级机构不存在");
//        }
//        public bool Delete(int id)
//        {
//            if (CheckOrganizationUnit(c => c.ParentId == id)) throw new MessageException("机构包含下级");
//            return Repository.Delete<OrganizationUnit>(id);
//        }
//        public Dictionary<int, string> GetDictionary(int userId, UserAutoType type)
//        {
//            return SDC.OrganizationUnits.DataFilter(userId, type).ToDictionary(c => c.Id, c => c.Name);
//        }
//    }
//}
